package com.weetop.cfw

import android.os.Bundle
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.*
import com.gyf.immersionbar.ImmersionBar
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.other.CommonWebViewActivity
import com.weetop.cfw.utils.MMKVUtils
import com.weetop.cfw.widget.SplashAgreementDialog
import com.weetop.cfw.widget.SplashAgreementDialog.onClickButtonListener
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit


/**
 * 欢迎页
 */
class SplashActivity : CommonBaseActivity() {

    private  var dispose:Disposable? = null

    override fun getLayoutId() = R.layout.activity_splash

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).transparentStatusBar().fitsSystemWindows(false).statusBarDarkFont(true).init()
        ScreenUtils.setFullScreen(this@SplashActivity)
        LogUtils.e(MMKVUtils.getAgreement())
        if(MMKVUtils.getAgreement()){
            initPermission()
        }else {
            val splashAgreementDialog = SplashAgreementDialog.newInstance()
            splashAgreementDialog.setOnClickButtonListener(object : onClickButtonListener {
                override fun onClickAgreeContinue() {
                    MMKVUtils.saveAgreement(true)
                    initPermission()
                }

                override fun onClickExitApp() {
                    finish()
                }

                override fun onClickPrivacy() {
                    CommonWebViewActivity.startCommonWebViewActivity(
                        this@SplashActivity,
                        UrlConstants.privacy_policy_web_url
                    )
                }

                override fun onClickLicence() {
                    CommonWebViewActivity.startCommonWebViewActivity(
                        this@SplashActivity,
                        UrlConstants.use_protocol_web_url
                    )
                }
            })
            splashAgreementDialog.show(supportFragmentManager, "SplashAgreementDialog")
        }

    }

    private fun initPermission(){
        PermissionUtils.permission(PermissionConstants.STORAGE,PermissionConstants.LOCATION,PermissionConstants.PHONE)
            .callback(object:PermissionUtils.FullCallback{
                override fun onGranted(permissionsGranted: MutableList<String>?) {
                    timingStart()
                }

                override fun onDenied(
                    permissionsDeniedForever: MutableList<String>?,
                    permissionsDenied: MutableList<String>?
                ) {
                    timingStart()
                }

            }).request()
    }

    override fun initData(savedInstanceState: Bundle?) {
    }

    /**
     * 定时跳转
     */
    private fun timingStart(){
        if(dispose != null && !dispose!!.isDisposed){
            dispose!!.dispose()
        }
        dispose = Observable.timer(2000,TimeUnit.MILLISECONDS).subscribe {
            MainActivity.startMainActivity(this,0)
            ActivityUtils.finishActivity(this@SplashActivity)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
       if(dispose != null){
           if(!dispose!!.isDisposed){
               dispose?.dispose()
           }
       }
    }
}
